﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System.ComponentModel.DataAnnotations;

namespace FarmSmartSleep.Organ.API.Application.PromptReportCommands
{
    public class PendingSelectHandler : IRequestHandler<PendingSelectCommand, Result>
    {
        private readonly IBaseRepository<SleepInfor> sleepinforRep;
        private readonly IBaseRepository<Cottages> cottagesRep;
        private readonly IBaseRepository<Fields> fieldsRep;

        public PendingSelectHandler(IBaseRepository<SleepInfor> sleepinforRep, IBaseRepository<Cottages> cottagesRep, IBaseRepository<Fields> fieldsRep)
        {
            this.sleepinforRep = sleepinforRep;
            this.cottagesRep = cottagesRep;
            this.fieldsRep = fieldsRep;
        }


        /// <summary>
        /// 待配提示显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(PendingSelectCommand request, CancellationToken cancellationToken)
        {
            var quest = new Result() { Code = (int)CodeEnum.success };

            var list = (from a in sleepinforRep.GetAll()
                        join b in cottagesRep.GetAll() on a.RecordcottageId equals b.CottagesId
                        join c in fieldsRep.GetAll() on a.FieIdId equals c.FieIdId
                        select new PendingDTO()
                        {
                            SleepInforId = a.SleepInforId,
                            MoEarNumber = a.MoEarNumber,
                            CottagesId = b.CottagesId,
                            CottageName = b.CottageName,
                            FieIdId = c.FieIdId,
                            FieIdName = c.FieIdName,
                            BreedStatuId = a.BreedStatuId,
                            SleepEntryDate = a.SleepEntryDate,
                        });

            quest.Code = (int)CodeEnum.success;
            quest.Data = list;

            return quest;
        }



        public class PendingDTO
        {
            public int SleepInforId { get; set; }//档案编号
            /// <summary>
            /// 母耳号
            /// </summary>
            public string? MoEarNumber { get; set; }
            /// <summary>
            /// 栋舍id
            /// </summary>
            public int CottagesId { get; set; }
            /// <summary>
            /// 栋舍名称
            /// </summary>
            public string CottageName { get; set; }
            /// <summary>
            /// 栏位id
            /// </summary>
            public int FieIdId { get; set; }
            /// <summary>
            /// 栏位名称
            /// </summary>
            [StringLength(30)]
            public string? FieIdName { get; set; }
            /// <summary>
            /// 入场日期
            /// </summary>
            public DateTime SleepEntryDate { get; set; }
            /// <summary>
            /// 繁殖状态
            /// </summary>
            public int? BreedStatuId { get; set; }
            /// <summary>
            /// 配种的日期
            /// </summary>
            public DateTime BreedingDate { get; set; }
        }
    }
}
